package com.itany.shop.service.proxy;

import com.itany.shop.exception.ProductNotExistException;
import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.exception.UserNotExistException;
import com.itany.shop.exception.UserNotLoginException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.CartService;
import com.itany.shop.service.ProductService;
import com.itany.shop.tran.TransactionManager;
import com.itany.shop.vo.Cart;

/**
 * @author Miss Chen
 * @version 1.0
 * @date 2024年08月15日16:49
 */
public class CartServiceProxy implements CartService {
    private TransactionManager tran= (TransactionManager) ObjectFactory.getObject("tran");
    private CartService cartService= (CartService) ObjectFactory.getObject("cartServiceTarget");
    @Override
    public void addCart(Cart cart, Integer id) throws RequestParameterErrorException, ProductNotExistException, UserNotLoginException {
        try {
            tran.begin();
            cartService.addCart(cart,id);
            tran.commit();
        } catch (UserNotLoginException e) {
            tran.rollback();
            throw e;
        } catch (RequestParameterErrorException e) {
            tran.rollback();
            throw e;
        } catch (ProductNotExistException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void removeCart(Cart cart, Integer id) throws RequestParameterErrorException, UserNotLoginException {
        try {
            tran.begin();
            cartService.removeCart(cart,id);
            tran.commit();
        } catch (UserNotLoginException e) {
            e.printStackTrace();
            throw e;
        } catch (RequestParameterErrorException e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public void modifyCart(Cart cart, Integer id, Integer num) throws RequestParameterErrorException, UserNotLoginException {
        try {
            tran.begin();
            cartService.modifyCart(cart,id,num);
            tran.commit();
        } catch (UserNotLoginException e) {
            e.printStackTrace();
            throw e;
        } catch (RequestParameterErrorException e) {
            e.printStackTrace();
            throw e;
        }
    }
}
